package jsu.xxy.coffee3.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jsu.xxy.coffee3.domain.*;
import jsu.xxy.coffee3.mapper.MaterialMapper;
import jsu.xxy.coffee3.mapper.SupplierMaterialMapper;
import jsu.xxy.coffee3.service.SupplierService;
import jsu.xxy.coffee3.mapper.SupplierMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 张从轩
* @description 针对表【supplier】的数据库操作Service实现
* @createDate 2023-06-03 06:43:14
*/
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier>
implements SupplierService{
    @Autowired
    SupplierMapper supplierMapper;

    @Autowired
    MaterialMapper materialMapper;

    @Autowired
    SupplierMaterialMapper supplierMaterialMapper;
    public int deleteAll(List<Ids> ids){
        int ans = 0;
        for(int i = 0 ; i < ids.size() ; i ++){
            ans += supplierMapper.deleteByIds(ids.get(i).getMid() , ids.get(i).getSupplierid());
        }
        return ans;
    }

//    public boolean changeAll(SupplierAndMaterials supplierAndMaterials) {
////        获取供应商,更新供应商
//        Supplier supplier = new Supplier(supplierAndMaterials.getSupplierid() , supplierAndMaterials.getName() ,supplierAndMaterials.getAddress(),supplierAndMaterials.getPhone());
//        supplierMapper.updateByAll(supplier);
//        List<SupplierAndMaterials> s = supplierMaterialMapper.getByIds(supplierAndMaterials.getSupplierid(),supplierAndMaterials.getMid());
//        Material material2 = materialMapper.selectByName(supplierAndMaterials.getMname());
//        SupplierMaterial supplierMaterial = new SupplierMaterial(supplierAndMaterials.getSupplierid() , material2.getMid() , supplierAndMaterials.getPurchaseprice());
//        supplierMaterialMapper.updateByAll(supplierMaterial);
//        return true;
//    }

    public boolean addInform(SupplierAndMaterials supplierAndMaterials) {
        int an = 0;
        String supplierName = supplierAndMaterials.getName();//供应商id
        System.out.println(supplierName);
        System.out.println(supplierName);
        System.out.println(supplierName);
        System.out.println(supplierName);
        System.out.println(supplierName);
        List<Supplier> supplierAndMaterials1 = supplierMapper.selectByNamePhoneAddress(supplierAndMaterials.getName(),supplierAndMaterials.getPhone(),supplierAndMaterials.getAddress());
        System.out.println(supplierAndMaterials1);
        System.out.println(supplierAndMaterials1);
        int supplierid = 0;
//        没有，加供应商
        if(supplierAndMaterials1.size()==0){
            System.out.println(supplierAndMaterials.getName());
            supplierMapper.addSupplier(supplierAndMaterials.getName(),supplierAndMaterials.getAddress(),supplierAndMaterials.getPhone());


        }
        List<Supplier> ss = supplierMapper.selectByNamePhoneAddress(supplierAndMaterials.getName(),supplierAndMaterials.getPhone(),supplierAndMaterials.getAddress());
        System.out.println(ss);
        supplierid = ss.get(0).getSupplierid();
        Material material = materialMapper.selectByName(supplierAndMaterials.getMname());
        System.out.println(ss);
        int id = 0;
        if(material == null) {
            materialMapper.add(supplierAndMaterials.getMname() , 0);
        }
        id = materialMapper.selectByName(supplierAndMaterials.getMname()).getMid();//获得原材料的id
        System.out.println(id);
        List<SupplierMaterial> s = supplierMaterialMapper.getByIds(supplierid,id);
        System.out.println();
        System.out.println(supplierid);
        System.out.println(id);
        if(s.size() != 0) return false;
        return supplierMaterialMapper.insertAll(new SupplierMaterial(supplierid,id,supplierAndMaterials.getPurchaseprice())) > 0;
    }

    @Override
    public Page<SupplierAndMaterials> getAll(Integer pageNo, Integer pageSize) {
        List<SupplierAndMaterials> s  =  supplierMapper.selectOnePage((pageNo -1)*pageSize,pageSize);
        Page<SupplierAndMaterials> h = new Page<>();
        h.setTotal(supplierMapper.selectAllcount());
        h.setRecords(s);
        return h;
    }

    @Override
    public List<SupplierAndMaterials> getAllNoPage() {
        List<SupplierAndMaterials> s  =  supplierMapper.selectAll();
        return s;
    }
    @Override
    public Page<SupplierAndMaterials> selectByName(String name, Integer pageNum, Integer pageSize) {
        Page<SupplierAndMaterials> page = new Page<>();
        long lenn = supplierMapper.getCount(name);
        page.setTotal(lenn);
        page.setTotal(lenn);
        page.setTotal(lenn);
        page.setTotal(lenn);
        List<SupplierAndMaterials> s = supplierMapper.selectByName(name,(pageNum - 1) * pageSize, pageSize);

        page.setRecords(s);
        return page;
    }

    @Override
    public List<SupplierAndMaterials> selectByNameSupplier(String nameSupplier, Integer pageNo, Integer pageSize) {
        List<SupplierAndMaterials> s = supplierMapper.selectByNameSupplier(nameSupplier,(pageNo - 1) * pageSize, pageSize);
        return s;
    }

    @Override
    public boolean deleteById(Integer mid, Integer supplierid) {

        return supplierMapper.deleteByIds(mid , supplierid) > 0;
    }
}
